import {
  LOCATION_CHANGE,
} from './actions'

/**
 * 基于 history 返回一个 reducer 处理函数\
 * reducer 处理函数用于处理路径变化与 store 仓库同步
 */
export function createRouterReducer(history) {
  const initialState = {
    action: history.action,
    location: history.location,
  }
  return function(state = initialState, action) {
    if (action.type === LOCATION_CHANGE) { // 路径发生变化 同步到 store 中
      return {
        ...state,
        location: action.payload.location,
        action: action.payload.action,
      }
    } else {
      return state
    }
  }
}
